<?php

namespace Home\Controller;

class ImageController extends CommonController
{
    //显示相册列表页面
    public function index()
    {
        $user_id=I('get.id',$_SESSION['home']['uid'],'int');
        if ($_GET['id'] != $_SESSION['home']['uid']) {
            $user_info = M('user_info');
            $res = $user_info -> field('nickname') -> where('user_id='.$_GET['id'])->select();
            $res = $res[0]['nickname'];
            $this->assign('vnickname',$res.'的');
        }
        $albums = M('Albums');
        $photos = M('Photos');
        $albumsList = $albums->where('user_id='.$user_id)->select();
        foreach($albumsList as $key => $value){
            $count=$photos->where('albums_id='.$value['albums_id'])->group('albums_id')->count();
            if($count===null){
                $count=0;
            }
            $albumsList[$key]['photos_num']=$count;
        }
        $this->assign('albumsList', $albumsList);
        $this->assign('curUserId',$user_id);
        $this->assign('test', '相册');
        $this->display();
    }

    //相册添加处理用ajax脚本
    public function albumsadddo()
    {
        if (IS_AJAX) {
            $rules = array(
                array('albums_name', 'require', '请填写相册名称！'),
                array('albums_type', '2,4', '请选择相册类型！', 0, 'between', 1),
            );
            $data = array(
                'albums_name' => I('get.name'),
                'albums_type' => I('get.type'),
                'user_id' => $_SESSION['home']['uid'],
                'time' => $_SERVER['REQUEST_TIME'],
            );
            $albums = M('albums');
            if ($albums->validate($rules)->create($data)) {
                $id = $albums->add();
                $result = array(
                    'status' => 1,
                    'id' => $id,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'info' => $albums->getError(),
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //相册编辑处理用ajax脚本
    public function albumseditdo()
    {
        if (IS_AJAX) {
            $albums_id=I('get.id',0,'int');
            $user_id=$_SESSION['home']['uid'];
            $albums = M('albums');
            $res=$albums->where('albums_id='.$albums_id.' and user_id='.$user_id)->count();
            if($res){
                $rules = array(
                    array('albums_name', 'require', '请填写相册名称！'),
                    array('albums_type', '2,4', '请选择相册类型！', 0, 'between', 1),
                );
                $data = array(
                    'albums_id'   => $albums_id,
                    'albums_name' => I('get.name'),
                    'albums_type' => I('get.type'),
                    'user_id' => $user_id,
                );
                if ($albums->validate($rules)->create($data)) {
                    $albums->save();
                    $result = array('status' => 1);
                } else {
                    $result = array(
                        'status' => 0,
                        'info' => $albums->getError(),
                    );
                }
            }else{
                $result = array(
                    'status' => 0,
                    'info' => '非法操作！',
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //相册删除处理用ajax脚本    相册连同相片一起删除
    public function albumsdeldo()
    {
        if (IS_AJAX) {
            $photos = M('Photos');
            $albums=M('Albums');
            $id = I('get.id');
            $user_id = $_SESSION['home']['uid'];
            $res=$albums->where('albums_id='.$id.' and user_id='.$user_id)->count();
            if($res){
                if ($albums->where('albums_type>1 and user_id='.$user_id)->delete($id)) {
                    //删除相片文件
                    $photosList=$photos->where('albums_id='.$id)->select();
                    foreach($photosList as $value){
                        if(file_exists('./Public'.$value['photos_path'])){
                            unlink('./Public'.$value['photos_path']);
                        }
                    }
                    //删除相片信息
                    $photos->where('albums_id='.$id)->delete();
                    $result = array('status' => 1);
                } else {
                    $result = array(
                        'status' => 0,
                    );
                }
            }else{
                $result = array(
                    'status' => 0,
                );
            }
            $this->ajaxReturn($result);
        }
    }

    //显示对应相册中的相片列表
    public function image()
    {
        $id = I('get.id', 0, 'int');
        if($id){
            $photos = M('Photos');
            $albums=M('Albums');
            $res=$albums->field('user_id,albums_type')->where('albums_id='.$id)->find();
            //获取相册持有人
            $user_id=$res['user_id'];
            //相册是别人的情况
            if($user_id!=$_SESSION['home']['uid']){
                //获取相册类型
                $albums_type=$res['albums_type'];
                switch($albums_type){
                    case 2: //仅相册持有人可见
                        $this->error('您没有权限查看该相册！');
                        break;
                    case 3: //仅相册持有人好友可见
                        $ring=M('Ring');
                        $a=$ring->where('user_id_a='.$user_id.' and user_id_b='.$_SESSION['home']['uid'])->count();
                        $b=$ring->where('user_id_a='.$_SESSION['home']['uid'].' and user_id_b='.$user_id)->count();
                        if(!$a || !$b){
                            $this->error('您不是他好友，没有权限查看该相册！');
                        }
                        break;
                }
            }

            $photosList = $photos->where('albums_id='.$id)->select();
            $this->assign('photosList', $photosList);
            $this->assign('test', '相册');
            $this->assign('curUserId',$user_id);
            $this->display();
        }
    }

    //在对应相册中添加相片的处理页面
    public function imageadddo()
    {
        $photos = M('Photos');
        $albums=M('Albums');
        $id=I('post.id', 0, 'int');
        $user_id = $_SESSION['home']['uid'];
        $res=$albums->where('albums_id='.$id.' and user_id='.$user_id)->count();
        if($res){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Public/'; // 设置附件上传根目录
            $upload->savePath = 'Uploads/photo/'; // 设置附件上传目录
            // 上传文件
            $info = $upload->uploadOne($_FILES['file']);
            if ($info) {
                // 上传成功
                $data = array(
                    'albums_id' => $id,
                    'photos_name' => $info['name'],
                    'photos_info' => 'Ta很懒~什么都没写~',
                    'photos_zan' => 0,
                    'photos_path' => '/'.$info['savepath'].$info['savename'],
                    'time' => $_SERVER['REQUEST_TIME'],
                );
                if ($photos->create($data)) {
                    $photos->add($data);
                }
            }
        }
    }

    //删除对应相片需要调用的ajax脚本
    public function imagedeldo()
    {
        if (IS_AJAX) {
            $id = I('get.id',0,'int');
            $photos=M('Photos');
            $res=$photos->field('albums_id')->where('photos_id='.$id)->find();
            if($res){
                $albums_id=$res['albums_id'];
                $user_id = $_SESSION['home']['uid'];
                $albums=M('Albums');
                $cunzai=$albums->where('albums_id='.$albums_id.' and user_id='.$user_id)->count();
                if($cunzai){
                    //删除照片
                    $photosInfo=$photos->where('photos_id='.$id)->find();
                    if($photos->where('photos_id='.$id)->delete()){
                        if(file_exists('./Public'.$photosInfo['photos_path'])){
                            unlink('./Public'.$photosInfo['photos_path']);
                        }
                        $result = array('status' => 1);
                    }else{
                        $result = array('status' => 0);
                    }
                }else{
                    $result = array('status' => 0);
                }
            }else{
                $result = array('status' => 0);
            }
            $this->ajaxReturn($result);
        }
    }

    //图片赞
    public function photoszan(){
        $id=I('get.id',0,'int');
        if($id){
            $photos=M('Photos');
            $photos->where('photos_id='.$id)->setInc('photos_zan');
        }
    }

    //编辑图片信息
    public function photoseditdo(){
        $id=I('get.id',0,'int');
        $photos=M('Photos');
        $res=$photos->field('albums_id')->where('photos_id='.$id)->find();
        if($res){
            $albums_id=$res['albums_id'];
            $user_id = $_SESSION['home']['uid'];
            $albums=M('Albums');
            $cunzai=$albums->where('albums_id='.$albums_id.' and user_id='.$user_id)->count();
            if($cunzai){
                $data=array(
                    'photos_id'=>$id,
                    'photos_name'=>I('get.name','未命名','string'),
                    'photos_info'=>I('get.info','','string')
                );
                if($photos->create($data)){
                    $photos->save($data);
                    $result = array('status' => 1);
                }else{
                    $result = array('status' => 0);
                }
            }else{
                $result = array('status' => 0);
            }
        }else{
            $result = array('status' => 0);
        }
        $this->ajaxReturn($result);
    }
}
